package cn.groupmarket.domain.activity.service.discount;

import cn.groupmarket.domain.activity.adapter.repository.IActivityRepository;
import cn.groupmarket.domain.activity.model.valobj.DiscountTypeEnum;
import cn.groupmarket.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.groupmarket.domain.trade.adapter.repository.ITradeOrderRepository;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService {

    @Resource
    private IActivityRepository activityRepository;


    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        // 1. 人群标签过滤
        if (DiscountTypeEnum.TAG.equals(groupBuyDiscount.getDiscountType())){
            boolean isCrowdRange = filterTagId(userId, groupBuyDiscount.getTagId());
            if (!isCrowdRange){
                log.info("不在优惠人群范围内 {}" ,userId);
                return originalPrice;
            }
        }
        // 2. 折扣优惠计算
        return doCalculate(originalPrice, groupBuyDiscount);
    }


    private boolean filterTagId(String userId,String tagId){
        return activityRepository.isTagCrowRange(tagId,userId);
    }

    protected abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount);
}
